import {
  getPageListData,
  deletePageItem,
  createPageData,
  editPageData
} from '@/service/main/system/system'
import { Module } from 'vuex'
import { IRootStore } from '../types'
import { ISystemState } from './types'

const systemModule: Module<ISystemState, IRootStore> = {
  namespaced: true,
  state() {
    return {
      usersList: [],
      usersCount: 0,
      roleList: [],
      roleCount: 0,
      goodsList: [],
      goodsCount: 0,
      menuList: []
    }
  },
  mutations: {
    changeUsersList(state, userList: any[]) {
      state.usersList = userList
    },
    changeUsersCount(state, userCount: number) {
      state.usersCount = userCount
    },
    changeRoleList(state, roleList: any[]) {
      state.roleList = roleList
    },
    changeRoleCount(state, roleCount: number) {
      state.roleCount = roleCount
    },
    changeGoodsList(state, goodList: any[]) {
      state.goodsList = goodList
    },
    changeGoodsCount(state, goodsCount: number) {
      state.goodsCount = goodsCount
    },
    changeMenuList(state, menuList: any[]) {
      state.menuList = menuList
    }
  },
  actions: {
    async getPageListAction({ commit }, payload: any) {
      // 1.根据不同的情况发送不同的url请求
      const pageName: string = payload.pageName
      const pageUrl = `${pageName}/list`
      // let pageUrl = ''
      // switch (pageName) {
      //   case 'user':
      //     pageUrl = '/users/list'
      //     break
      //   case 'role':
      //     pageUrl = 'role/list'
      //     break
      // }

      const listResult = await getPageListData(pageUrl, payload.queryInfo)
      console.log(listResult)

      const { list, totalCount } = listResult.data
      // 设置不同的数据  首字符转成大写
      commit(
        `change${pageName.replace(/^\S/, (s) => s.toUpperCase())}List`,
        list
      )
      if (totalCount) {
        commit(
          `change${pageName.replace(/^\S/, (s) => s.toUpperCase())}Count`,
          totalCount
        )
      }
    },
    async deletePageItemAction({ dispatch }, payload: any) {
      const url = `/${payload.pageName}/${payload.id}`
      await deletePageItem(url)

      // 重新拉取一下新的列表数据
      dispatch('getPageListAction', {
        pageName: payload.pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    },
    async createPageDataAction({ dispatch }, payload: any) {
      const { pageName, newData } = payload

      await createPageData(pageName, newData)
      // 重新拉取一下新的列表数据
      dispatch('getPageListAction', {
        pageName: payload.pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    },
    async editPageDataAction({ dispatch }, payload: any) {
      const { pageName, editData, id } = payload
      console.log(editData)

      const pageUrl = `${pageName}/${id}`
      await editPageData(pageUrl, editData)

      // 重新拉取一下新的列表数据
      dispatch('getPageListAction', {
        pageName: payload.pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    }
  },
  getters: {
    pageListData(state): (pageName: string) => any {
      return (pageName: string) => {
        // 想要拼接 usersList 但是拼接的话，ts检测不通过，所以只能暂时转成any，然后再取
        return (state as any)[`${pageName}List`]

        // switch (pageName) {
        //   case 'users':
        //     return state.usersList
        //     break
        //   case 'role':
        //     return state.roleList
        //     break
        // }
      }
    },
    pageCountData(state): (pageName: string) => any {
      return (pageName: string) => {
        return (state as any)[`${pageName}Count`]
      }
    }
  }
}

export default systemModule
